<?php

namespace App\Modules\Admin\Controllers;


use App\Models\Resource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ResourceController extends Controller
{
    public function index()
    {
        $resource = Resource::query()->get();
        return view('admin::resource.index', ['resource' => $resource]);
    }

    public function get()
    {
        return response()->json(Resource::query()->get());
    }

    public function first(Request $request)
    {
        $id = $request->id;
        return response()->json(Resource::find($id));
    }

    public function save(Request $request)
    {
        $params = $request->all();
        $rule   = [
            'sort' => 'numeric|max:99'
        ];
        Validator::make($params, $rule)->validate();
        if (isset($params['id'])) {
            Resource::query()->where('id',$params['id'])->update($params);
        } else {
            Resource::create($params);
        }
    }

    public function delete(Request $request)
    {
        Resource::destroy($request->input('id'));
    }
}
